package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/** 
 * ModifyWindow is the window which pops when the user wants to edit the information about a contact
 * @author Johanna
 *
 */
public class ModifyWindow extends JFrame {
	private JTextField fnameField = new JTextField(20);
	private JTextField lnameField = new JTextField(20);
	private JTextField numberField = new JTextField(20);
	private JTextField addressField = new JTextField(20);
	private JTextField emailField = new JTextField(20);
	private JTextField homepageField = new JTextField(20);

	private JPanel labelsPane;
	private JPanel fieldsPane;
	private JPanel buttonsPane;

	private JButton saveButton;

	public static final String FIRSTNAME = "First name:";
	public static final String LASTNAME = "Last name:";
	public static final String NUMBER = "Number:";
	public static final String ADDRESS = "Address:";
	public static final String GROUP = "Group:";
	public static final String EMAIL = "E-mail:";
	public static final String HOMEPAGE = "Homepage:";
	public static final int NB_OF_FIELDS = 7;
	public static final String SAVE = "Save the changes";
	public static final String TITLE = "Edit a contact";
	public static final String FORM_TITLE = "EDIT THE CONTACT";
	public final String[] GROUPS = { "- Select a group -", "Family", "Work", "Friends", "Clients" };
	
	private Window mainWindow;

	private JComboBox groupField = new JComboBox(GROUPS);

	/**
	 * COnstructor
	 * @param view
	 */
	public ModifyWindow(Window view) {
		
		setTitle(TITLE);
		
		this.mainWindow = view;
		
		// Pane with the titles of the fields
		labelsPane = new JPanel(new GridLayout(NB_OF_FIELDS,1,5,5));

		labelsPane.add(new JLabel(FIRSTNAME));
		labelsPane.add(new JLabel(LASTNAME));
		labelsPane.add(new JLabel(NUMBER));
		labelsPane.add(new JLabel(ADDRESS));
		labelsPane.add(new JLabel(GROUP));
		labelsPane.add(new JLabel(EMAIL));
		labelsPane.add(new JLabel(HOMEPAGE));

		// Pane with the text fields
		fieldsPane = new JPanel(new GridLayout(NB_OF_FIELDS,1,5,5));

		fieldsPane.add(this.fnameField);
		fieldsPane.add(this.lnameField);
		fieldsPane.add(this.numberField);
		fieldsPane.add(this.addressField);
		fieldsPane.add(this.groupField);
		fieldsPane.add(this.emailField);
		fieldsPane.add(this.homepageField);

		this.reset();

		// Submit button
		this.buttonsPane = new JPanel(new GridLayout(1,2,5,5));

		this.saveButton = new JButton(); 
		this.saveButton.setText(SAVE); 
		buttonsPane.add(this.saveButton);

		// Add paddings
		labelsPane.setBorder(new EmptyBorder(5, 5, 5, 5) );
		fieldsPane.setBorder(new EmptyBorder(5, 5, 5, 5) );
		buttonsPane.setBorder(new EmptyBorder(0, 5, 5, 5) );

		// Organize the layout of the form
		Container contentPane = getContentPane();
		((JComponent) contentPane).setBorder(new EmptyBorder(15, 15, 15, 15));
		this.setSize(600,330);
		setLayout(new BorderLayout(5,5));
		add(labelsPane, BorderLayout.WEST);
		add(fieldsPane, BorderLayout.CENTER);
		add(buttonsPane, BorderLayout.SOUTH);
		add(new JLabel(FORM_TITLE), BorderLayout.NORTH);
	}

	public void run()
	{
		this.setVisible(true);
	}
	
	public void addSaveModifyListener(ActionListener saveModifyListener) {
		this.getSaveButton().addActionListener(saveModifyListener);
	}

	public String getFnameField() {
		return this.fnameField.getText();
	}

	public String getLnameField() {
		return this.lnameField.getText();
	}

	public String getNumberField() {
		return this.numberField.getText();
	}

	public String getAddressField() {
		return this.addressField.getText();
	}

	public String getGroupField() {
		return (String)this.groupField.getSelectedItem();
	}

	public String getEmailField() {
		return this.emailField.getText();
	}

	public String getHomepageField() {
		return this.homepageField.getText();
	}

	public void setFnameField(String newValue) {
		this.fnameField.setText(newValue);
	}

	public void setLnameField(String newValue) {
		this.lnameField.setText(newValue);
	}

	public void setNumberField(String newValue) {
		this.numberField.setText(newValue);
	}


	public void setAddressField(String newGroup) {
		this.addressField.setText(newGroup);
	}

	public void setEmailField(String newValue) {
		this.emailField.setText(newValue);
	}

	public void setHomepageField(String newValue) {
		this.homepageField.setText(newValue);
	}

	public JButton getSaveButton() {
		return this.saveButton;
	}
	
	public JComboBox getComboGroups()
	{
		return this.groupField;
	}

	/** 
	 * Method to reset the fields of the form
	 */
	public void reset() {
		this.fnameField.setText("");
		this.lnameField.setText("");
		this.numberField.setText("");
		this.addressField.setText("");
		this.groupField.setSelectedIndex(0);
		this.emailField.setText("");
		this.homepageField.setText("");
	}

}